package fr.compil.tab.ihm;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.management.InvalidAttributeValueException;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.compil.tab.logic.CellManager;
import fr.compil.tab.model.Board;
import fr.compil.tab.model.Cell;


public class CellListener extends JPanel implements ListSelectionListener, KeyListener{
	
	private Tableur tableur;
	private FormuleBox formule;
	private JTable table;
	private JTextField text;
	private String internalFormule;
	private Board board;
	
	public CellListener(Tableur tableur, FormuleBox formule, Board board){
		
		this.tableur = tableur;
		this.formule = formule;
		this.board = board;
		table = tableur.getTable();
		text = formule.getFormuleBox();
		
		internalFormule = "";
		
		//Listener du changement de cellule
		ListSelectionModel listSelectionModel = table.getSelectionModel();        
		listSelectionModel.addListSelectionListener(this);
		table.getColumnModel().getSelectionModel().addListSelectionListener(this);
		
		text.addKeyListener(this);
		
	}

		
		@Override
		public void valueChanged(ListSelectionEvent e) {
//			// TODO Auto-generated method stub
//			if (e.getValueIsAdjusting())
//	            return;
//	        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
//	        if (lsm.isSelectionEmpty()) {
//	        	//No cell selected
//	        }
//	        else{
//	        	internalFormule = "";
//	        	Object o = table.getValueAt(table.getSelectedRow(),table.getSelectedColumn());
//	        	if (o != null)
//	        		internalFormule = o.toString();
//	        	else formule = null;
////	        	text.setText(internalFormule);
	        //}
		}


		@Override
		public void keyPressed(KeyEvent arg0) {
			// TODO Auto-generated method stub
			/*if(arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE){
				internalFormule = internalFormule.substring(0, internalFormule.length()-1);
				table.setValueAt(internalFormule, table.getSelectedRow(), table.getSelectedColumn());
			}
			else{
				if(Character.isLetterOrDigit(arg0.getKeyChar()) || (arg0.getKeyChar()) == '/' || (arg0.getKeyChar()) == '+' || (arg0.getKeyChar()) == '-' || (arg0.getKeyChar()) == '*'){
					//Ecrire dans la cellule
					internalFormule += String.valueOf(arg0.getKeyChar());
					table.setValueAt(internalFormule, table.getSelectedRow(), table.getSelectedColumn());
				}*/
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					board.getBoard()[table.getSelectedRow()-1][table.getSelectedColumn()-1].setRawValue(text.getText());
					try {
						int value = CellManager.processTokenStack(CellManager.sliceValue(text.getText()));
						board.getBoard()[table.getSelectedRow()-1][table.getSelectedColumn()-1].setProcessedValue(value);
					} catch (InvalidAttributeValueException e) {
						e.printStackTrace();
					}
					table.setValueAt(board.getBoard()[table.getSelectedRow()-1][table.getSelectedColumn()-1].getProcessedValue(),table.getSelectedRow(), table.getSelectedColumn());
					
					for (int i = 1; i < 200; i++) {
						for (int j = 1; j < 27; j++) {
							Cell cell = board.getBoard()[i-1][j-1];
							try {
								cell.refreshValue();
							} catch (InvalidAttributeValueException e) {
								e.printStackTrace();
							}
							table.setValueAt(cell.getProcessedValue(), i, j);
						}
					}
				}
			}
		//}


		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

}
